package com.aplikata.evok.ui;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.SelectEvent;

import com.aplikata.bo.Domain;
import com.aplikata.constants.ConstantsMsg;
import com.aplikata.evok.bo.EvokService;
import com.aplikata.evok.datamodel.ServiceDataModel;
import com.aplikata.evok.service.EvokTherapyService;
import com.aplikata.ui.UIBean;
import com.aplikata.util.FacesUtil;

/**
 * @ClassName: EvokServiceBean
 * @Description: TODO
 * @author Huyun
 * @date May 25, 2012 12:23:10 PM
 */
@ManagedBean(name = "evokServiceBean")
@ViewScoped
public class EvokServiceBean extends UIBean {
	private static final long serialVersionUID = 1L;

	@ManagedProperty(value = "#{evokTherapyService}")
	private EvokTherapyService therapyService;

	private List<EvokService> serviceList;
	private ServiceDataModel serviceModel;

	private EvokService service;
	private Domain domain;
	public final static String WDG_SERVICE_LIST = "wdgServiceList";

	public EvokServiceBean() {
		domain = FacesUtil.getDomainFromSession();
		service = new EvokService(domain);
	}

	@Override
	public void domainChange() {
		super.domainChange();
		serviceList = null;
		serviceModel = new ServiceDataModel(therapyService, this.getServiceList());
	}

	public void submit() {
		if (service == null)
			return;

		try {
			if (service.getDomain() == null) {
				service.setDomain(FacesUtil.getDomainFromSession());
			}
			service = (EvokService) therapyService.updateService(service);
			FacesUtil.addInfoMessage(ConstantsMsg.INFO_SUCCESS, new Object[] { service.getName() });
			service = new EvokService(domain);
			serviceList = null;
			serviceModel = new ServiceDataModel(therapyService, this.getServiceList());
			FacesUtil.unselectTableRows(WDG_SERVICE_LIST);
		} catch (Exception e) {
			FacesUtil.addActionListenerException(e, log);
		}
	}

	public void delete() {
		if (service == null || FacesUtil.isKeyNull(service.getId()))
			return;

		try {
			therapyService.deleteByKey(EvokService.class, service.getId());
			serviceList.remove(service);
			service = new EvokService(domain);
		} catch (Exception e) {
			FacesUtil.addActionListenerException(e, log);
		}
	}

	public void add() {
		service = new EvokService();
		FacesUtil.unselectTableRows(WDG_SERVICE_LIST);
	}

	public void selectService(SelectEvent event) {
		FacesUtil.resetInput("mainform:serviceInfo");
		service = (EvokService) event.getObject();
	}

	public EvokTherapyService getTherapyService() {
		return therapyService;
	}

	public void setTherapyService(EvokTherapyService therapyService) {
		this.therapyService = therapyService;
	}

	@SuppressWarnings("unchecked")
	public List<EvokService> getServiceList() {
		if (serviceList == null) {
			if (getSessionDomain() == null)
				return null;

			serviceList = (List<EvokService>) therapyService.findByNamedQuery(EvokService.QY_FIND_BY_DOMAIN,
					new Object[] { this.getSessionDomain().getId() });
		}
		return serviceList;
	}

	public void setServiceList(List<EvokService> serviceList) {
		this.serviceList = serviceList;
	}

	public ServiceDataModel getServiceModel() {
		if (serviceModel == null) {
			serviceModel = new ServiceDataModel(therapyService, this.getServiceList());
		}
		return serviceModel;
	}

	public void setServiceModel(ServiceDataModel serviceModel) {
		this.serviceModel = serviceModel;
	}

	public EvokService getService() {
		return service;
	}

	public void setService(EvokService service) {
		this.service = service;
	}

}
